package com.xiyue.leaspring.util;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 * 定义一个用于操作JPA的工厂程序类，主要功能是负责
 * EntityManager与EntityManagerFactory接口的对象管理
 * @author xiyue
 *
 */
public class JPAEntityFactory {
	private static final String PERSISTENCE_UNIT = "LEASPRING";//持久化单元
	private static EntityManagerFactory entityManagerFactory;//定义连接工厂
	private static ThreadLocal<EntityManager> entityThreadLocal =
			new ThreadLocal<EntityManager>();//保存EntityManager接口对象
	static {//静态代码块获取EntityManagerFactory实例
		rebuildEntityManagerFactory();//实例化EntityManagerFactory接口对象
	}
	
	private JPAEntityFactory() {}
	/**
	 * 该方法的主要功能是获取EntityManagerFactory接口对象
	 */
	private static void rebuildEntityManagerFactory() {
		entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
	}

	/**
	 * 获取EntityManagerFactory接口对象
	 * @return EntityManagerFactory接口实例
	 */
	public static EntityManagerFactory getEntityManagerFactory() {
		if(entityManagerFactory==null) {
			rebuildEntityManagerFactory();
		}
		return entityManagerFactory;
	}
	
	/**
	 * 获取EntityManager对象,不同的线程获取各自的EntityManager接口对象
	 * @return EntityManager接口实例
	 */
	public static EntityManager getEntityManager() {
		EntityManager entityManager = entityThreadLocal.get();//获取EntityManager接口对象
		if(entityManager==null) {//如果没有实例化对象
			if(entityManagerFactory==null) {//未连接工厂
				rebuildEntityManagerFactory();//创建工厂实例
			}//创建新的EntityManager接口对象
			entityManager = entityManagerFactory.createEntityManager();
			entityThreadLocal.set(entityManager);//保存对象信息
		}
		return entityManager;
	}
	
	public static void close() {
		EntityManager entityManager = entityThreadLocal.get();
		if(entityManager!=null) {//已保存EntityManager
			entityManager.close();
			entityThreadLocal.remove();//从ThreadLocal中删除对象
		}
	}
	
}
